import 'package:flutter/material.dart' ;
import 'package:flutter/gestures.dart' ;






void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Text Width Example'),
        ),
        body: MyTextWidget(),
      ),
    );
  }
}

class MyTextWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyTextWidgetState();
  }

}
class MyTextWidgetState extends State<MyTextWidget> {

  @override
  void initState() {

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        print("点击超过300就响应");
      },
      child: Container(
        padding: EdgeInsets.all(20),
        color: Colors.blue,
        child: OnlyOnePointerRecognizerWidget(
          onTap: (){
            print("tap");
          },
          child: Container(
            color: Colors.red,
            width: 300,
            height: 300,
          ),
        ),
      ),
    );
  }
}


class OnlyOnePointerRecognizer extends TapGestureRecognizer {

  @override
  bool isPointerAllowed(PointerDownEvent event) {
    if (event.position.dy > 300) {
      return false;
    }
    return super.isPointerAllowed(event);
  }
}

class OnlyOnePointerRecognizerWidget extends StatelessWidget {
  final Widget? child;
  final GestureTapCallback? onTap;
  OnlyOnePointerRecognizerWidget({ this.child, this.onTap });

  @override
  Widget build(BuildContext context) {
    return RawGestureDetector(
        gestures: <Type, GestureRecognizerFactory>{
          OnlyOnePointerRecognizer: GestureRecognizerFactoryWithHandlers<OnlyOnePointerRecognizer>(
                  () => OnlyOnePointerRecognizer(),
                  (OnlyOnePointerRecognizer instance) {
                    if(onTap!= null) {
                      instance.onTap = onTap!;
                    }
                  }
          )
        },
        child: child
    );
  }
}

